using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Server;
using TXLooker.Core;

namespace TXLooker.UI
{
    public partial class TFServerConnectionForm : Form
    {
        public IEnumerable<ServerConfigItem> ServerConfigItems
        {
            get { return this.m_items; }
        }
        private List<ServerConfigItem> m_items = new List<ServerConfigItem>();

        private TFServerConnectionForm()
        {
            InitializeComponent();
        }
        public TFServerConnectionForm(IEnumerable<ServerConfigItem> items) : this()
        {
            foreach( ServerConfigItem item in items )
                this.m_items.Add( item.Clone() as ServerConfigItem );
        }

        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad(e);

            this.m_cbServers.SelectedIndexChanged += new EventHandler( OnServersSelectedIndexChanged );
            this.m_chblistProjects.ItemCheck += new ItemCheckEventHandler( OnItemCheck );

            this.ReloadServersUI(this.m_items);
        }
        private void OnSelectAll( object sender, EventArgs e )
        {
            if( !object.ReferenceEquals(this.m_chbSelectAll, sender) )
                throw new NotImplementedException();

            for( int i = 0; i < this.m_chblistProjects.Items.Count; i++ )
                this.m_chblistProjects.SetItemChecked( i, this.m_chbSelectAll.Checked );
        }
        private void OnServers( object sender, EventArgs e )
        {
            ServerListEditForm form = new ServerListEditForm( this.m_items );
            form.ShowDialog();

            this.m_cbServers.DataSource = null;
            this.m_items.Clear();
            this.m_items.AddRange( form.Servers );

            this.ReloadServersUI( this.m_items );
        }
        private void OnServersSelectedIndexChanged( object sender, EventArgs e )
        {
            this.m_projectListPanel.Enabled = false;

            if( this.m_cbServers.SelectedItem == null )
                return;

            ServerConfigItem serverItem = this.m_cbServers.SelectedItem as ServerConfigItem;

            //clear previous data
            this.m_chblistProjects.Items.Clear();

            IEnumerable<ProjectInfo> projects = TeamFoundationDataProvider.GetProjects( serverItem.ServerURI );

            this.m_projectListPanel.Enabled = (projects != null);

            if( projects == null ) return;

            bool bSelectAll = true;
            bool bCheckItem = false;

            foreach( ProjectInfo project in projects )
            {
                bCheckItem = serverItem.ProjectURIs.Contains(project.Uri);
                bSelectAll &= bCheckItem;

                CheckBoxListItemWrapper<ProjectInfo> item = new CheckBoxListItemWrapper<ProjectInfo>( project, "Name" );
                this.m_chblistProjects.Items.Add( item, bCheckItem );
            }

            this.m_chbSelectAll.Checked = bSelectAll;
        }
        private void OnItemCheck( object sender, ItemCheckEventArgs e )
        {
            bool bChecked = (e.NewValue == CheckState.Checked);

            string uri = ( this.m_chblistProjects.Items[e.Index] as CheckBoxListItemWrapper<ProjectInfo> ).Item.Uri;

            ServerConfigItem serverItem = (ServerConfigItem)this.m_cbServers.SelectedItem;

            if( !bChecked )
            {
                serverItem.ProjectURIs.Remove(uri);
            }
            else
            {
                if( !serverItem.ProjectURIs.Contains(uri) )
                    serverItem.ProjectURIs.Add(uri);
            }
        }

        private void ReloadServersUI( IEnumerable<ServerConfigItem> items )
        {
            this.m_cbServers.DataSource = null;
            this.m_cbServers.DataSource = items;
            this.m_cbServers.DisplayMember = "ServerURI";
        }

    }
}